package com.base.kongjian.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.dao.WorkShareRecDao;
import com.base.kongjian.entity.WorkShareRec;
import com.base.kongjian.service.PopularitySystemService;
import com.base.kongjian.service.WealthSystemService;
import com.base.kongjian.service.WorkShareRecService;
import com.mysteel.page.Page;

@Service("workShareRecService")
public class WorkShareRecServiceImpl implements WorkShareRecService
{
	@Autowired
	private WorkShareRecDao workShareRecDao;
	@Autowired
	private PopularitySystemService popularitySystemService;
	@Autowired
	private WealthSystemService wealthSystemService;

	
	@Override
	public void saveWSR(WorkShareRec wsr)
	{	
		Assert.notNull (wsr);
		
		workShareRecDao.save (wsr);
	}
	
	@Override
	public void deleteWSRById(long id)
	{
		WorkShareRec wsr = workShareRecDao.get (id);
		if(ObjectUtils.notNull (wsr))
		{
			workShareRecDao.delete (id);
		}	
	}
	
	@Override
	public WorkShareRec getWSRById(long id)
	{
		
		return workShareRecDao.get(id);
	}
	
	@Override
	public Page<WorkShareRec> queryWSR(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		
		Page<WorkShareRec> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = workShareRecDao.count(params);

		if (count > 0)
		{
			page = new Page<WorkShareRec>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> wsrIds = workShareRecDao.query(params);

			if (ObjectUtils.notEmpty(wsrIds))
			{
				List<WorkShareRec> wsrs = new ArrayList<WorkShareRec>(wsrIds.size());

				for (long wsrId : wsrIds)
				{
					wsrs.add(getWSRById (wsrId));
				}

				page.setDatas(wsrs);
			}
		}

		return page;
	}

	@Override
	public boolean addShareRec(long workId, int workKind, int type,long publisherId, long sharerId)
	{
		Map<String,Object> params = new HashMap<String,Object>();
		params.put ("workId" ,workId);
		params.put ("type" ,type);
		params.put ("workKind" ,workKind);
		params.put ("sharerId" ,sharerId);
		
		WorkShareRec wsr = getWSRByMap (params);
		
		if(ObjectUtils.isNull (wsr)||(type==1))
		{
			wsr = new WorkShareRec ();
			wsr.setPublisherId (publisherId);
			wsr.setSharerId (sharerId);
			wsr.setType (type);
			wsr.setWorkId (workId);
			wsr.setWorkKind (workKind);
			wsr.setShareTime (System.currentTimeMillis ());
			
			popularitySystemService.addPopularityValue (publisherId ,type,1,workId);
			wealthSystemService.WealthtransformSystem (publisherId , workId, type,1);	
			
			saveWSR (wsr);
			
			return true;
		}	
		
		return false;
	}

	@Override
	public WorkShareRec getWSRByMap(Map<String, Object> params)
	{
		
		return workShareRecDao.getByMap (params);
	}

	@Override
	public void deleteWSRByMap(Map<String, Object> params)
	{
		Page<WorkShareRec> page = queryWSR (params ,0 ,-1);
		
		if(ObjectUtils .notNull (page))
		{
			for(WorkShareRec wsr : page.getDatas ())
			{
				deleteWSRById (wsr.getId ());
			}
		}
	}

	@Override
	public void updateWSR(WorkShareRec wsr)
	{
		Assert.notNull (wsr);
		WorkShareRec ws = workShareRecDao.get (wsr.getId ());
		if(ObjectUtils.notNull (ws))
		{
			ws.setShareTime (wsr.getShareTime ());
			workShareRecDao.update(ws);
		}
	}
}
